package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 地址管理
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        log.info("新增地址为: {}", addressBook.toString());

        //获取当前用户id
        Long userId = (Long) request.getSession().getAttribute("user");
        //将当前用户id赋值给addressBook
        addressBook.setUserId(userId);
        //新增地址
        addressBookService.save(addressBook);

        return R.success(addressBook);
    }

    /**
     * 地址展示
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //addressBook.setUserId(userId);
        log.info("用户id为: {}", userId);

        //创建条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(null != userId, AddressBook::getUserId, userId);
        //queryWrapper.eq(AddressBook::getIsDeleted, 0);
        //根据最新修改时间排序
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址的id为: {}", addressBook.getId());

        //创建条件构造器LambdaUpdateWrapper
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault, 0);

        addressBookService.update(queryWrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 根据id回显地址数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id) {
        log.info("回显修改地址数据的id为: {}", id);

        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("没有找到对象");
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        log.info("修改的数据为: {}", addressBook.toString());

       /* LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,addressBook.getId());

        addressBookService.update(addressBook,queryWrapper);*/

        addressBookService.updateById(addressBook);


        return R.success("修改地址成功");
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除地址的id: {}", ids.toString());

       /* LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(AddressBook::getId, ids);
        updateWrapper.set(ids.size() != 0,AddressBook::getIsDeleted, 1);
        addressBookService.update(updateWrapper);*/

        addressBookService.removeByIds(ids);

        return R.success("删除地址成功");
    }

    /**
     * 获取用户默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        queryWrapper.eq(AddressBook::getIsDeleted,0);

        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        return addressBook != null ? R.success(addressBook):R.error("暂无默认地址");
    }
}
